<?php
class Direk
{

    /**
     * Construct
     *
     * Opens the database and initializes the object.
     *
     */
    public function __construct()
    {
        $this->db = new SQLiteDatabase( 'db/direk.sqlite' );
    }

    /**
     * Get All Contacts
     *
     * Returns an array containing all contacts.
     *
     * @return array
     */
    public function get_all_contacts()
    {
        $result = $this->db->query( 'SELECT rowid, fname, lname, email, phone FROM contacts ORDER BY lname, fname, email, phone' );
        if ( $result ) {
            return $result->fetchAll(); 
        } else {
            return false;
        }
    }

    /**
     * Filter Contacts
     *
     * Return a list of contacts that match the given search terms.
     *
     * @param string $terms
     * @return array
     */
    public function filter_contacts( $terms )
    {
        if ( ! $terms ) {
            return false;
        } else {
            foreach ( explode( ' ', $terms ) as $term ) {
                foreach ( array( 'fname', 'lname', 'email', 'phone' ) as $column ) {
                    $conditions_array[] = $column. ' LIKE "%'.$term.'%"';
                }
            }
            $conditions  = '( ';
            $conditions .= implode( ' OR ', $conditions_array );
            $conditions .= ' )';
            $result = $this->db->query( 'SELECT rowid, fname, lname, email, phone FROM contacts WHERE '.$conditions.' ORDER BY lname, fname, email, phone' );
            if ( $result ) {
                return $result->fetchAll();
            } else {
                return false;
            }
        } 
    }

    /**
     * Get Contact
     *
     * Returns an array with information for contact identified by $id. 
     *
     * @param integer $id
     * @return array
     */
    public function get_contact( $id ) 
    {
        if ( ! is_integer( $id )) {
            return false;
        }
        $result = $this->db->query( 'SELECT rowid, fname, lname, email, phone FROM contacts WHERE rowid = '.$id );
        if ( $result ) {
            return $result->fetch();
        } else {
            return false;
        }
    }

    /**
     * Create Contact
     *
     * Creates a new entry in the contacts table.
     *
     * @param string $fname First Name
     * @param string $lname Last Name
     * @param string $email Email Address
     * @param string $phone Phone Number
     * @return integer The ROWID of the new entry
     */
    public function create_contact( $fname, $lname, $email, $phone )
    {
        $rc = $this->db->queryExec( 'INSERT INTO contacts VALUES ( "'.sqlite_escape_string( $fname ).'", "'.sqlite_escape_string( $lname ).'", "'.sqlite_escape_string( $email).'", "'.sqlite_escape_string( $phone ).'" )' );
        if ( $rc ) {
            return $this->db->lastInsertRowid();
        } else {
            return false;
        }
    }

    /**
     * Update Contact
     *
     * Updates an existing entry in the contacts table.
     *
     * @param string $fname First Name
     * @param string $lname Last Name
     * @param string $email Email Address
     * @param string $phone Phone Number
     * @return integer The ROWID of the new entry
     */
    public function update_contact( $id, $fname, $lname, $email, $phone )
    {
        $rc = $this->db->queryExec( 'UPDATE contacts SET fname = "'.sqlite_escape_string( $fname ).'", lname = "'.sqlite_escape_string( $lname ).'", email = "'.sqlite_escape_string( $email).'", phone = "'.sqlite_escape_string( $phone ).'" WHERE rowid = '.$id );
        if ( $rc ) {
            return $this->db->lastInsertRowid();
        } else {
            return false;
        }
    }

    /**
     * Delete Contact
     *
     * Deletes a contact from the database. Return true on success, false on failure.
     *
     * @param integer $id
     * @return boolean
     */
    public function delete_contact( $id ) 
    {
        $rc = $this->db->queryExec( 'DELETE FROM contacts WHERE rowid = '.$id );
        if ( $rc ) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * Debug
     *
     * Prints a div with debugging and backtrace info.
     *
     * @param mixed $var
     * @return void
     */
    public function debug( $var ) {
        echo '<div class="debug" style="border: 1px dotted black; padding: .5em; font-family: courier, fixed-width; font-size: 10px;    white-space: pre;">';
        echo '<strong>';
        var_export( $var );
        echo '</strong>';
        echo "\n";
        debug_print_backtrace();
        echo '</div>'."\n";
    }
}
?>
